package com.bankcard.card;

import java.util.Objects;

public final class Name {
    private final String firstName;
    private final String lastName;
    private final String fullName;

    public Name(String firstName, String lastName) {
        this.firstName = Objects.requireNonNull(firstName, "First name cannot be null");
        this.lastName = Objects.requireNonNull(lastName, "Last name cannot be null");
        this.fullName = firstName + " " + lastName;
    }

    public Name(String fullName) {
        this.fullName = Objects.requireNonNull(fullName, "Full name cannot be null");
        String[] parts = fullName.split(" ", 2);
        if (parts.length == 2) {
            this.firstName = parts[0];
            this.lastName = parts[1];
        } else {
            this.firstName = null;
            this.lastName = null;
        }
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getFullName() {
        return fullName;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Name name = (Name) o;
        return Objects.equals(firstName, name.firstName) &&
                Objects.equals(lastName, name.lastName) &&
                fullName.equals(name.fullName);
    }

    @Override
    public int hashCode() {
        return Objects.hash(firstName, lastName, fullName);
    }

    @Override
    public String toString() {
        return fullName;
    }
}